﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BookingDetail : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (!IsPostBack)
        {
            try
            {
                BookingId = int.Parse(Request.QueryString["Id"]);
            }
            catch (FormatException)
            {
            }

            if (BookingId > 0 && Booking != null && Booking.UserId != null)
            {
                foreach (var field in details.Fields)
                {
                    if (field is Acms.WebControls.TextBoxField)
                    {
                        Acms.WebControls.TextBoxField tmp = (Acms.WebControls.TextBoxField)field;
                        tmp.ReadOnly = true;
                    }
                }
            }
        }

        dsBooking.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsBooking_Select);
        dsSlots.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsSlots_Select);
        dsBooking.Update += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsBooking_Update);
    }


    void dsBooking_Update(object sender, Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs e)
    {
        int id = (int)e.Keys["Id"];
        BrandUnion.CustomData.BookingFactory factory = new BrandUnion.CustomData.BookingFactory();
        var booking = factory.GetById(id);
        if (booking != null)
        {
            if (booking.UserId == null)
            {
                booking.Address1 = (string)e.NewValues["Address1"];
                booking.Address2 = (string)e.NewValues["Address2"];
                booking.County = (string)e.NewValues["County"];
                booking.EmailAddress = (string)e.NewValues["EmailAddress"];
                booking.FirstName = (string)e.NewValues["FirstName"];
                booking.LastName = (string)e.NewValues["LastName"];
                booking.PostCode = (string)e.NewValues["PostCode"];
                booking.Tel = (string)e.NewValues["Tel"];
                booking.Town = (string)e.NewValues["Town"];
                try
                {
                    booking.Age = int.Parse((string)e.NewValues["Age"]);
                }
                catch (FormatException) { }
            }
            booking.MoreInfo = (bool)e.NewValues["MoreInfo"];
            bool updateHeader = false;
            if ((string)e.NewValues["BookingSlotId"] != (string)e.OldValues["BookingSlotId"])
            {
                try
                {
                    booking.BookingSlotId = int.Parse((string)e.NewValues["BookingSlotId"]);
                    updateHeader = true;
                }
                catch (FormatException) { }
            }

            factory.Save(booking);

            _Booking = booking;
            BookingId = booking.Id;

            if (updateHeader)
            {
                //base.RebingBreadCrumbs();
            }
        }
    }

    void dsSlots_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        BrandUnion.CustomData.BookingSlotFactory factory = new BrandUnion.CustomData.BookingSlotFactory();
        var slots = factory.GetAll();
        var data = slots.Select(x => new { Name = string.Format("{0:ddd dd/MM/yyyy HH:mm} - {1} of {2} slots booked", x.DateTime, x.Booked, x.NoSlots), Value = x.Id }).ToArray();
        e.SetData(data);
    }

    void dsBooking_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(Booking);
    }

    

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private int BookingId { get; set; }
    private BrandUnion.CustomData.Booking _Booking;
    protected BrandUnion.CustomData.Booking Booking
    {
        get
        {
            if (_Booking == null && BookingId > 0)
            {
                _Booking = new BrandUnion.CustomData.BookingFactory().GetById(BookingId);
            }
            return _Booking;
        }
    }


    protected override object SaveViewState()
    {
        ViewState["BookingId"] = BookingId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        BookingId = (int)ViewState["BookingId"];

    }

    public override void GetExtaBreadCrumbs(Dictionary<string, string> extaItems)
    {
        base.GetExtaBreadCrumbs(extaItems);
        if (Booking != null)
        {
            extaItems.Add(string.Format("{1} {2} @ {0:ddd dd/MM/yyyy HH:mm}", Booking.DateTime, Booking.FirstName, Booking.LastName), this.Request.Url.PathAndQuery);
        }
        else if (BookingId == 0)
        {
            extaItems.Add("Create New Booking", this.Request.Url.PathAndQuery);
        }
    }
}
